package cn.wolfcode.trip.admin;

import cn.wolfcode.trip.core.domain.Strategy;
import cn.wolfcode.trip.core.query.RegionQueryObject;
import cn.wolfcode.trip.core.query.StrategyQueryObject;
import cn.wolfcode.trip.core.service.IRegionService;
import cn.wolfcode.trip.core.service.IStrategyService;
import cn.wolfcode.trip.core.util.JsonResult;
import cn.wolfcode.trip.core.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IRegionService regionService;

    @RequestMapping("list")
    public String Strategy(@ModelAttribute("qo") StrategyQueryObject qo, Model model) {
        RegionQueryObject regionQueryObject = new RegionQueryObject();
        regionQueryObject.setUndisable(true); // 查询不为禁用的数据
        model.addAttribute("regions", regionService.listAll(regionQueryObject));
        model.addAttribute("pageInfo", strategyService.query(qo));
        return "strategy/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Strategy strategy, MultipartFile file) {
        JsonResult json = new JsonResult();
        try {
            //上传图片
            if (file!=null&&file.getSize()>0) {
                String url = UploadUtil.uploadAli(file);
                strategy.setCoverUrl(url);
            }
            strategyService.saveOrUpdate(strategy);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }
}
